#include "resource.h"
#include "xmlview.h"
#include "exceptions.h"
#include <commctrl.h>
#include "settings.h"
#include "xmlcapture.h"
#include "filesys.h"
#include "tuxdocument.h"

/*  Make the class name into a global variable  */
char szClassName[ ] = "XmlEd";

using std::set_unexpected;
using std::unexpected_handler;

class STC // store and reset
{
	private:
		unexpected_handler old;
	public:
		STC(unexpected_handler f)
			{old = set_unexpected(f);}
		~STC()
			{set_unexpected(old);}
};

void Unexpected()
	{
  		MessageBox(0,"Some function threw an error not in its throw specification.",
        "Unexpected Error",MB_OK);
	};


int WINAPI
WinMain (HINSTANCE hThisInstance,
         HINSTANCE hPrevInstance,
         LPSTR lpszArgument,
         int nFunsterStil)

{
	STC unexpect(&Unexpected);
	try
	{
		InitCommonControls(); 	// make sure common control library is loaded.
		INITCOMMONCONTROLSEX iccx = { sizeof(INITCOMMONCONTROLSEX), 
			ICC_BAR_CLASSES | ICC_COOL_CLASSES };
		::InitCommonControlsEx(&iccx);
		

	    HWND hwnd;               /* This is the handle for our window */
	    MSG messages;            /* Here messages to the application are saved */
	    WNDCLASSEX wincl;        /* Data structure for the windowclass */

		sys::FilePath p;
		p.AssignToCurrentPath();
		p.RemoveLastPart();
  		doc::TuxDocument::Instance().Prefs.SetExePath(p);
	    /* The Window structure */
	    wincl.hInstance = hThisInstance;
	    wincl.lpszClassName = szClassName;
	    wincl.lpfnWndProc = view::XmlView::WndProc;      /* This function is called by windows */
	    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
	    wincl.cbSize = sizeof (WNDCLASSEX);

	    wincl.hIcon = LoadIcon (hThisInstance, "ICO_MAIN");
	    wincl.hIconSm = LoadIcon (hThisInstance, "ICO_MAIN");
	    wincl.lpszMenuName = "MNU_MAIN";
	    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
	    
	    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
	    wincl.cbWndExtra = 0;                      /* structure or the window instance */
	    wincl.hbrBackground = NULL; /* NO background painting for this window */

	    /* Register the window class, and if it fails quit the program */
	    if (!RegisterClassEx (&wincl))
	        return 0;

	    /* The class is registered, let's create the program*/
	    hwnd = CreateWindowEx(
	           0,                   /* Extended possibilites for variation */
	           szClassName,         /* Classname */
	           "TuxPad",       	/* Title Text */
	           WS_OVERLAPPEDWINDOW, /* default window */
	           CW_USEDEFAULT,       /* Windows decides the position */
	           CW_USEDEFAULT,       /* where the window ends up on the screen */
	           544,                 /* The programs width */
	           375,                 /* and height in pixels */
	           HWND_DESKTOP,        /* The window is a child-window to desktop */
	           NULL,                /* No menu */
	           hThisInstance,       /* Program Instance handler */
	           NULL                 /* No Window Creation data */
	           );

	    /* Make the window visible on the screen */
	    ShowWindow (hwnd, nFunsterStil);
        HWND dialog = view::TuxCapture::GetDialogHandle();
        
        /* open the file given as argument */
		if (lpszArgument != 0 && *lpszArgument != 0)
		{
			// if the filename has "'s, remove them
			if (*lpszArgument == '\"')
			{
				char * pchar = lpszArgument+1;
				while (*pchar != '\0')
				{
					*(pchar-1) = *pchar;
					pchar++;
				}
				pchar-=2;
				*pchar = '\0';
			}
			doc::TuxDocument::Instance().LoadFromFile(lpszArgument);
		}
	    while (GetMessage (&messages, NULL, 0, 0))
	    {
           if (!IsDialogMessage(dialog,&messages))
            {
    	        TranslateMessage(&messages);
    	        DispatchMessage(&messages);
	        }
	    }

	    /* The program return-value is 0 - The value that PostQuitMessage() gave */
	    return messages.wParam;
 	}
  	catch (std::exception &e)
 	{
  		MessageBox(NULL,e.what(),"Unrecoverable Error",MB_OK);
 	}
 	/*catch (...)
 	{
  		MessageBox(NULL,"Some function threw an non-exception error that "
    				"was not handled anywhere.","Unexpected Error",MB_OK);
 	}*/
	return 0; 	
}




